package com.bitcola.chainserver.server.inner;

import com.bitcola.chainserver.mapper.ColaChainDepositMapper;
import com.bitcola.chainserver.server.trx.TrxServer;
import com.bitcola.security.common.msg.AppResponse;
import com.bitcola.security.common.msg.ColaChainDepositResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

@RestController
@RequestMapping("blockchain/explorer")
@CrossOrigin

public class InnerController {

    @Autowired
    TrxServer trxServer;
    @Autowired
    InnerService innerService;
    @Autowired
    ColaChainDepositMapper depositMapper;
    @RequestMapping(value = "scan")
    public AppResponse scan(String param)
    {

        if(param.startsWith("lk")){
            var result=depositMapper.selectUserChainBalance(param);
            return AppResponse.ok().data(result);

        }else if(param.startsWith("0x")){
            Example example=new Example(ColaChainDepositResponse.class);
            example.createCriteria().andEqualTo("hash",param);
            return AppResponse.ok().data(depositMapper.selectByExample(example));

        }else if(param.matches("\\d+")){
            Example example=new Example(ColaChainDepositResponse.class);
            example.createCriteria().andEqualTo("blockNumber",Long.parseLong(param));
            example.orderBy("timestamp").desc();
            return AppResponse.ok().data(depositMapper.selectByExample(example));
        }
        return AppResponse.ok();
    }

    @RequestMapping(value = "blockNumber")
    public AppResponse getBlockNumber(){
        return AppResponse.ok().data(innerService.getCurrentBlockNumber());
    }

    public static void main(String[] args) {
        System.out.println("234234".matches("\\d+"));
    }
}

